import object_template from "./object_template.mjs";
import route_template from "./route_template.mjs";
import select_template from "./select_template.mjs";
import string_template from "./string_template.mjs";
import { nearest_data, nearest_param } from "./nearest.mjs";
import ajax_template from "./ajax_template.mjs";
import gridview_template from "./gridview_template.mjs";
import if_template from "./if_template.mjs";
import tab_template from "./tab_template.mjs";
import formview_template from "./formview_template.mjs";
import formviewitem_template from "./formviewitem_template.mjs";
import input_template from "./input_template.mjs";
import field_template from "./field_template.mjs";
import switch_template from "./switch_template.mjs";
import pager_template from "./pager_template.mjs";
import { datalist_template } from "./datalist.mjs";
import textarea_template from "./textarea_template.mjs";
import worksheet_template from "./worksheet_template.mjs";

export let template_ext = {};

export default function thin_render(container, template) {
  if (template === undefined) {
    console.warn("no template");
  } else if (Array.isArray(template)) {
    template.forEach((t) => {
      thin_render(container, t);
    });
  } else if (typeof template === "string") {
    string_template(container, template);
  } else if (typeof template === "function") {
    let result = template({
      container: container,
      data: nearest_data(container),
      param: nearest_param(container),
    });
    if (result !== undefined) {
      thin_render(container, result);
    }
  } else {
    let key = Object.keys(template)[0];
    switch (key) {
      case "datalist":
        datalist_template(container, template);
        break;
      case "worksheet":
        worksheet_template(container, template);
        break;
      case "gridview":
        gridview_template(container, template);
        break;
      case "formview":
        formview_template(container, template);
        break;
      case "block":
      case "f1":
      case "f2":
      case "f3":
        formviewitem_template(container, template);
        break;
      case "field":
        field_template(container, template);
        break;
      case "tab":
        tab_template(container, template);
        break;
      case "route":
        route_template(container, template);
        break;
      case "ajax":
        ajax_template(container, template);
        break;
      case "foreach":
        foreach_template(container, template);
        break;
      case "switch":
        switch_template(container, template);
        break;
      case "if":
        if_template(container, template);
        break;
      case "pager":
        pager_template(container, template);
        break;
      case "select":
        // select_template();
        select_template(container, template);
        break;
      case "textarea":
        textarea_template(container, template);
        break;
      case "input":
        input_template(container, template);
        break;
      default:
        if (template_ext[key])
          template_ext[key]({
            container,
            template,
            data: nearest_data(container),
          });
        else object_template(container, template);
        break;
    }
  }
}
